package com.example.abc.util;

import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * Created by Peter on 2018/5/5.
 */
public class ImageUtil {

    public static BufferedImage decodeToImage(String imageString) throws IOException {

        byte[] imagedata = DatatypeConverter.parseBase64Binary(imageString.substring(imageString.indexOf(",") + 1));
        BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imagedata));
//            ImageIO.write(bufferedImage, "png", new File("d:/img.png"));
        return bufferedImage;
    }

    public static byte[] toBytes(String imageString) {
        byte[] imagedata = DatatypeConverter.parseBase64Binary(imageString.substring(imageString.indexOf(",") + 1));
        return imagedata;
    }
}
